package com.woniuxy.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class SimpleProducer {
    //定义一个生产者
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory=new ConnectionFactory();
        //设置连接工厂信息
        connectionFactory.setHost("127.0.0.1");//可以不设置 默认就是localhost
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //获取连接对象
        Connection connection = connectionFactory.newConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //发送消息
        for (int i = 0; i <10 ; i++) {
            ////exchange如果不是用,需要传一个空字符串,不能传null
            channel.basicPublish("","simple1",null,("建立简单的消息模型"+i).getBytes());
            System.out.println("发送消息成功");
        }

        //关闭资源
        channel.close();
        connection.close();

    }
}
